package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.model.UserInfo;
import com.example.demo.server.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Admin
 * Date: 2023-03-19
 * Time: 16:21
 */

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

    @RequestMapping("/reg")
    public Object reg(String username,String password) {
        //非空效验
        if(!StringUtils.hasLength("username") || !StringUtils.hasLength("password")) {
            return AjaxResult.fail(-1,"非法的参数请求!");
        }

        int result = userService.add(username,password);
        if(result == 1) {
            return AjaxResult.success("添加成功!",1);
        }else {
            return AjaxResult.fail(-1,"添加失败!");
        }
    }

    @RequestMapping("/login")
    public int login(String username,String password) {
        //非空效验
        if(!StringUtils.hasLength("username") || !StringUtils.hasLength("password")) {
            return 0;
        }

       UserInfo userInfo =  userService.login(username,password);
        if(userInfo == null || userInfo.getId() <= 0) {
            return 0;
        }else {
            return 1;
        }
    }

}
